package com.bwda.dsrs.systemservice.domain.po.license;


import com.alibaba.fastjson.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * simple introduction
 *
 * @author wangju
 * @date 2019/9/10
 */
public class LicenseInfo {

	private static final String TIME_FORMAT="yyyy-MM-dd HH:mm:ss";

	private static final String VENDOR_FIELD="VendorInfo";

	private static final String SYSCONF_FIELD="SysConfInfo";

	private static final String SOFTWARE_FIELD="SoftwareInfo";

	private static final String COMPANY_FIELD="CompanyName";

	private static final String PUBDATE_FIELD="PublishedDate";

	private static final String PUBLISHER_FIELD="Publisher";

	private static final String CPU_FILED="Cpuid";

	private static final String ADDR_FIELD="HWaddr";

	private static final String EXPTIME_FILED="ExpTime";

	private static final String SOFTNAME_FILED="SoftwareName";

	private static final String VERSION_FIELD="Version";

	private static final String DYNAMIC_FILED="BaseSrv";

	private JSONObject jsonObj;

	public LicenseInfo(JSONObject jsonObject) {
		if(jsonObject!=null){
			this.jsonObj=jsonObject;
		}
	}

	public String getCompanyName() {
		return jsonObj.getJSONObject(VENDOR_FIELD).getString(COMPANY_FIELD);
	}

	public String getPublishedDate() {
		return jsonObj.getJSONObject(VENDOR_FIELD).getString(PUBDATE_FIELD);
	}

	public String getPublisher() {
		return jsonObj.getJSONObject(VENDOR_FIELD).getString(PUBLISHER_FIELD);
	}

	public String getCpuid() {
		return jsonObj.getJSONObject(SYSCONF_FIELD).getString(CPU_FILED);
	}

	public String getHWaddr() {
		return jsonObj.getJSONObject(SYSCONF_FIELD).getString(ADDR_FIELD);
	}

	public String getExpTime() {
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getString(EXPTIME_FILED);
	}

	public boolean isOutOfExpress(){
		DateFormat format=new SimpleDateFormat(TIME_FORMAT);
		try {
			Date expreTime=format.parse(getExpTime());
			if(expreTime.before(new Date())){
				return true;
			}
		} catch (ParseException e) {

		}
		return false;
	}

	public String getSoftwareName() {
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getString(SOFTNAME_FILED);
	}

	public String getVersion() {
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getString(VERSION_FIELD);
	}

	public JSONObject getDynamic() {
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getJSONObject(DYNAMIC_FILED);
	}

	public Boolean getDynamicBoolean(String key){
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getJSONObject(DYNAMIC_FILED).getBoolean(key);
	}

	public Integer getDynamicInteger(String key){
		return jsonObj.getJSONObject(SOFTWARE_FIELD).getJSONObject(DYNAMIC_FILED).getInteger(key);
	}

}
